package com.zzyl.common.utils;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.core.auth.Auth;
import com.baidubce.qianfan.model.chat.ChatResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class AiUtil {
    @Value("${QianfanAi.api}")
    private String api;

    @Value("${QianfanAi.secret}")
    private String secret;

    @Value("${QianfanAi.model}")
    private String model;

    public String Chat(String prompt) {
        log.info("开始调用AI");
        Qianfan qianfan = new Qianfan(Auth.TYPE_OAUTH, api, secret);
        ChatResponse response = qianfan.chatCompletion().addMessage("user", prompt) //角色信息参考API文档，单个只能用user
                .model(model).temperature(0.7).maxOutputTokens(2000).responseFormat("json_object").execute();
        String result = response.getResult();
        log.info("AI返回结果：{}", result);
        return result;
    }
}
